package com.gxa.day2022_07_20;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MainFrame {


    Account account;
    void lauchFrame(Account account) {

        this.account = account;
        JFrame mainFrame = new JFrame("主界面");
        mainFrame.setSize(600, 400);
        mainFrame.setLocationRelativeTo(null);//居中
        mainFrame.setLayout(null);//不需要布局管理器
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        addWidget2Frame(mainFrame);//添加组件到mainFrame

        mainFrame.setVisible(true);
    }

    private void addWidget2Frame(JFrame mainFrame) {

        JLabel balanceLabel = new JLabel("您的余额是:"+ this.account.getBalance());
        balanceLabel.setBounds(100, 50, 200, 20);


        JLabel priceLabel = new JLabel("金额：");
        priceLabel.setBounds(50, 100, 50, 20);
        JTextField priceField = new JTextField();
        priceField.setBounds(110, 100, 150, 20);

        ButtonGroup buttonGroup = new ButtonGroup();
        JRadioButton saveButton = new JRadioButton("存");
        saveButton.setBounds(130, 140, 80, 20);
        saveButton.setSelected(true);//设置默认选中的是哪个
        JRadioButton getButton = new JRadioButton("取");
        getButton.setBounds(220, 140, 80, 20);
        buttonGroup.add(saveButton);
        buttonGroup.add(getButton);

        JButton comfirmButton = new JButton("确定");
        comfirmButton.setBounds(100,180,80,20);
        comfirmButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String money = priceField.getText();

                double money1 = Double.parseDouble(money);

                if(saveButton.isSelected()){
                    //存
                    account.save(money1);
                }else {
                    account.get(money1);
                }
                balanceLabel.setText("您的余额是:"+ account.getBalance());
            }
        });

        mainFrame.add(balanceLabel);
        mainFrame.add(priceLabel);
        mainFrame.add(priceField);
        mainFrame.add(saveButton);
        mainFrame.add(getButton);
        mainFrame.add(comfirmButton);
    }
}
